
package de.carcophan.jimdb.jsf.mb.util;

import com.flicklib.service.ProxyConfig;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;


@ApplicationScoped
public class ProxyConfigAppBean implements ProxyConfig, Serializable {

    private Configuration props;
    private HttpHost proxy;
    private AuthScope authScope;
    private Credentials credentials;

    @PostConstruct
    public void initConfiguration() {
        try {
            props = new PropertiesConfiguration("config.properties");
        } catch (ConfigurationException ex) {
            Logger.getLogger(ProxyConfigAppBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public HttpHost getHttpHost() {
        if (proxy == null) {
            proxy = new HttpHost(props.getString("proxy.host"), props.getInt("proxy.port"));
        }
        return proxy;
    }

    @Override
    public AuthScope getAuthScope() {
        if (authScope == null) {
            authScope = new AuthScope(props.getString("proxy.host"), props.getInt("proxy.port"));
        }
        return authScope;
    }

    @Override
    public Credentials getCredentials() {
        if (credentials == null) {
            credentials = new NTCredentials(props.getString("proxy.username"), props.getString("proxy.password"), props.getString("proxy.authentication.ntlm.workstation"), props.getString("proxy.authentication.ntlm.domain"));
        }
        return credentials;
    }
}
